package com.kikyou.service.impl;

import com.kikyou.pojo.Nation;
import com.kikyou.mapper.NationMapper;
import com.kikyou.service.INationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuankaiqiang
 * @since 2022-03-07
 */
@Service
public class NationServiceImpl extends ServiceImpl<NationMapper, Nation> implements INationService {

    @Autowired(required = false)
    private NationMapper nationMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @author     ：ltm
     * @date       ：2022/3/7
     * @time       : 10:10 PM
     * @description：查询所有的民族
     * @Param:
     * @return: 民族列表的范型
     */
    @Override
    public List<Nation> findAllNation() {
        ListOperations listOperations=redisTemplate.opsForList();
        //从redis中获取民族数据
        List<Nation> nations=listOperations.range("nation",0,-1);  ;
        if (CollectionUtils.isEmpty(nations)){
            nations=nationMapper.findAllNation();
            listOperations.rightPushAll("nation",nations);


        }
        return nations;

    }
}
